package public

import (
	"MembershipRegist/db"
	"MembershipRegist/models"
)

func (c *Controller) UserLogin() {
	username := c.GetString("username")
	password := c.GetString("password")
	if username == "" || password == "" {
		c.ResponseFailed(201, "用户名或密码不能为空")
		return
	}

	var user models.User
	if value := db.DB.Where("username = ? and password = ?", username, password).First(&user); value.Error != nil {
		c.ResponseFailed(202, "用户名或密码错误")
		return
	}

	c.SetSession("user_id", user.ID)
	c.ResponseSuccess()
	return
}

func (c *Controller) UserRegister() {
	username := c.GetString("username")
	password := c.GetString("password")
	nickname := c.GetString("nickname")
	studentID := c.GetString("student_id")
	if username == "" || password == "" || nickname == "" || studentID == "" {
		c.ResponseFailed(201, "请输入所有参数")
		return
	}

	user := models.User{
		Username:  username,
		Password:  password,
		Nickname:  nickname,
		StudentID: studentID,
	}

	if value := db.DB.Create(&user); value.Error != nil {
		c.ResponseFailed(202, "注册失败")
		return
	}

	c.ResponseSuccess()
	return
}
